<#import "../common/layout.ftl" as layout/>
<#import "../common/dataTables.ftl" as dataTables/>
<#import "../common/modern-forms.ftl" as mform/>
<#import "../common/modal.ftl" as modal/>
<!DOCTYPE html>
<html>
<@layout.head title="领取管理">
    <@dataTables.head/>
    <@dataTables.buttons_head/>
    <@dataTables.select_head/>
    <@dataTables.fixedHeader_head/>
    <@mform.head/>
    <@select.head/>
</@layout.head>
<body>
<@layout.page>
<div class="main-box">
    <div class="content">
        <div id="searchGroup" class="btn-group" style="position: absolute; ">


            <div style='float:left;margin:5px' id="warehouseBox">

            </div>

        </div>
        <!-- 表格 -->
        <div style="position: relative; top:40px; display: flex">

            <@dataTables.commonTable id="mainTable"/>
            <!-- 按钮 -->
            <div id="btnGroup" class="btn-group" style="position: absolute; top:0px;">
                <div>
                    <span>新增时间</span>
                    <input id="startdate" name="startdate" style="width: 100px" placeholder='请选择开始日期'/>
                    <span>至</span>
                    <input id="enddate" name="enddate" style="width: 100px" placeholder='请选择结束日期'/>

                    <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm"
                            style="margin-left: 5px;"
                            onclick="javascript:refreshData1();">
                        <i class="fa fa-search"></i> 查询
                    </button>
                    <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm" style="margin-left: 5px;"
                            onclick="javascript:newdetail();">
                        <i class="fa fa-plus"></i> 添加申请
                    </button>

                    <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm" style="margin-left: 5px;"
                            onclick="javascript:editdetail();">
                        <i class="fa fa-edit"></i> 修改
                    </button>
                    <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm" style="margin-left: 5px;"
                            onclick="javascript:checkdetail();">
                        <i class="fa fa-check"></i> 审核
                    </button>
                    <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm" style="margin-left: 5px;"
                            onclick="javascript:printGet();">
                        <i class="fa fa-print"></i> 打印申请单
                    </button>

                    <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm" style="margin-left: 5px;"
                            onclick="javascript:deldetail();">
                        <i class="fa fa-minus"></i> 删除
                    </button>

                </div>
            </div>
        </div>
        <div style="position: relative;display: flex; top:70px; ">

            <div style="position: relative;flex: 1;min-width: 450px;">
                <@dataTables.commonTable id="mainTable2"/>
                <!-- 按钮 -->
                <div id="btnGroup" class="btn-group" style="position: absolute; top:-30px;">
                    <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm" style="margin-left: 5px;"
                            onclick="javascript:refreshData2();">
                        <i class="fa fa-refresh"></i> 刷新
                    </button>
                    <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm" style="margin-left: 5px;"
                            onclick="javascript:newdetailList();">
                        <i class="fa fa-plus"></i> 添加明细
                    </button>

                    <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm" style="margin-left: 5px;"
                            onclick="javascript:getChangeInfo();">
                        <i class="fa fa-arrow-right"></i> 生成出库单
                    </button>
                    <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm" style="margin-left: 5px;"
                            onclick="javascript:editdetailList();">
                        <i class="fa fa-edit"></i> 修改
                    </button>
                    <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm" style="margin-left: 5px;"
                            onclick="javascript:deldetailList();">
                        <i class="fa fa-minus"></i> 删除
                    </button>

                </div>
            </div>

            <div style="position: relative;flex: 2; margin-left: 20px;">

                <@dataTables.commonTable id="mainTable3"/>
                <!-- 按钮 -->
                <div id="btnGroup" class="btn-group" style="position: absolute; top:-30px;">
                    <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm" style="margin-left: 5px;"
                            onclick="javascript:refreshData3();">
                        <i class="fa fa-refresh"></i> 刷新
                    </button>
                    <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm" style="margin-left: 5px;"
                            onclick="javascript:printChange();">
                        <i class="fa fa-print"></i> 打印领取单
                    </button>
                    <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm" style="margin-left: 5px;"
                            onclick="javascript:changeUpdate();">
                        <i class="fa fa-check"></i> 出库生效
                    </button>
                    <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm" style="margin-left: 5px;"
                            onclick="javascript:printQRCode();">
                        <i class="fa fa-qrcode"></i> 打印二维码
                    </button>
                    <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm" style="margin-left: 5px;"
                            onclick="javascript:delChangeInfo();">
                        <i class="fa fa-minus"></i> 删除
                    </button>
                </div>
            </div>
        </div>

    </div>
</div>
<!-- 弹出窗 领取单-->
    <@modal.editModal id="modal-detail" buttonId="btndetailSave1" width="800px" >
        <@mform.container id="editForm1">
        <input id="id" name="id" type="hidden"/>
        <input id="warehouseId" name="warehouseId" type="hidden"/>
        <input id="flowCode" name="flowCode" type="hidden"/>
            <@mform.row>
                <@mform.select2 id='useType' name='useType' label="使用类型"  size=4>
                <option value="">请选择使用类型</option>
                    <#list userType as item>
                    <option value="${item.code}">${item.name}</option>
                    </#list>
                </@mform.select2>
                <@mform.select2 id='getStaffId' name='getStaffId' label="领取人"  onchange="getStaffIdChange()" size=4>
                    <option value="">请选择领取人</option>
                </@mform.select2>
                <@mform.input_text id='getStaffTel' name='getStaffTel' size=4 label='领取人电话' placeholder='领取人电话' icon='fa-edit' />
            </@mform.row>

            <@mform.row>
                <@mform.select2 id='useOrgId' name='useOrgId' label="使用部门"  onchange="useOrgIdChange()" size=6>
                    <option value="">请选择使用部门</option>
                </@mform.select2>
                <@mform.select2 id='useStaffId' name='useStaffId' label="使用人" onchange="useStaffIdChange()"  size=6>
                    <option value="">请选择使用人</option>
                        <#list staffList as item>
                        <option value="${item.id?c}">${item.name}[${item.telephone}]</option>
                        </#list>
                </@mform.select2>
            </@mform.row>
            <@mform.row>
                <@mform.input_text id='usePosition' name='usePosition' size=6 label='使用位置' placeholder='使用位置' icon='fa-edit' />
                <@mform.input_text id='useTel' name='useTel' size=6 label='联系电话' placeholder='联系电话' icon='fa-edit' />
            </@mform.row>
            <@mform.row>
                <@mform.input_textarea id='reason' name='reason' size=12 label='使用原因' placeholder='100个字以内'  />
            </@mform.row>
        </@mform.container>
    </@modal.editModal>
<!-- 弹出窗 审核-->
    <@modal.editModal id="modal-detail3" buttonId="btndetailSave3" width="800px" >
        <@mform.container id="editForm3">
        <input id="id" name="id" type="hidden"/>
            <@mform.select2 id='checkOrgId' name='checkOrgId' label="审核部门"  onchange="checkOrgIdChange()" size=6>
            <option value="">请选择审核部门</option>
            </@mform.select2>
            <@mform.select2 id='checkStaffId' name='checkStaffId' label="审核人"  size=12>
            <option value="">请选择审核人</option>
            </@mform.select2>
            <@mform.datetime id='checkTime' name='checkTime' title="审核时间"  size=12>
            </@mform.datetime>
        </@mform.container>
    </@modal.editModal>
<!-- 弹出窗 明细-->
    <@modal.editModal id="modal-detail2" buttonId="btndetailSave2" width="600px" >
        <@mform.container id="editForm2">
        <input id="id" name="id" type="hidden"/>
        <input id="getId" name="getId" type="hidden"/>
            <@mform.select2 id='oneCode' name='oneCode' label="资产类型"  size=12>
            <option value="">请选择资产</option>
                <#list eqClass as item>
                <option value="${item.code}">${item.name}</option>
                </#list>
            </@mform.select2>
            <@mform.select2 id='brandCode' name='brandCode' label="品牌"  size=12>
            <option value="">请选择品牌</option>
                <#list brand as item>
                <option value="${item.code}">${item.name}</option>
                </#list>
            </@mform.select2>
            <@mform.select2 id='modelId' name='modelId' label="型号名称"  size=12>
            <option value="">请选择型号</option>
            </@mform.select2>
            <@mform.input_text id='getCount' name='getCount' size=12 label='申请数量' placeholder='申请数量' icon='fa-edit' />
            <@mform.input_text id='tips' name='tips' size=12 label='备注' placeholder='备注' icon='fa-edit' />
        </@mform.container>
    </@modal.editModal>

<!-- 弹出窗 出库单-->
    <@modal.editModal id="modal-detail4" buttonId="btndetailSave4" width="600px" >
        <@mform.container id="editForm4">
        <input id="warehouseId" name="warehouseId" type="hidden"/>
        <input id="getId" name="getId" type="hidden"/>
        <input id="id" name="id" type="hidden"/>
        <input id="modelId" name="modelId" type="hidden"/>
        <input id="flowCode" name="flowCode" type="hidden"/>
            <@mform.input_text readonly="true" id='typeName' name='typeName' size=12 label='资产类型' placeholder='资产类型' icon='fa-lock' />
            <@mform.input_text readonly="true" id='brandName' name='brandName' size=12 label='品牌' placeholder='品牌' icon='fa-lock' />
            <@mform.input_text readonly="true" id='name' name='name' size=12 label='型号名称' placeholder='型号名称' icon='fa-lock' />
            <@mform.input_text readonly="true" id='getCount' name='getCount' size=12 label='申请数量' placeholder='申请数量' icon='fa-lock' />
            <@mform.input_text readonly="true" id='tips' name='tips' size=12 label='备注' placeholder='备注' icon='fa-lock' />
            <@mform.input_text id='count' name='count' size=12 label='出库数量' placeholder='实际出库数量' icon='fa-edit' />
        </@mform.container>
    </@modal.editModal>


</@layout.page>
<@layout.foot>
    <@dataTables.foot/>
    <@dataTables.buttons_foot/>
    <@dataTables.select_foot/>
    <@dataTables.fixedHeader_foot/>
    <@mform.foot/>
<script src="${request.contextPath}/assets/js/jquery.serializejson.min.js?v=3653060703"></script>
<script type="text/javascript">
    var now = new Date();
    var nowStr=now.getFullYear() + "-" + (now.getMonth() + 1) + "-"+now.getDate();
    var begin = now.getFullYear() + "-" + (now.getMonth() + 1) + "-1";
    var endD;
    var nextMonth = now.getMonth() + 1;
    var nextMonthFirstDay = new Date(now.getFullYear(), nextMonth, 1);
    var oneDay = 1000 * 60 * 60 * 24;
    endD = new Date(nextMonthFirstDay - oneDay);
    var end = endD.getFullYear() + "-" + (endD.getMonth() + 1) + "-" + endD.getDate();
    $("#startdate").datetimepicker({
        language: "zh-CN",
        startView: 2,
        minView: 2,
        format: "yyyy-mm-dd",
        autoclose: true,
        initialDate: new Date(),
        todayBtn: true
    });
    $("#startdate").val(begin);
    $("#enddate").datetimepicker({
        language: "zh-CN",
        startView: 2,
        minView: 2,
        format: "yyyy-mm-dd",
        autoclose: true,
        todayBtn: true
    });
    $("#enddate").val(end);
    var orgInfo =${organizationName};
//    var orgSelect = $("<select id='oneOrganization' style='width:500px;' onchange='refreshWarehouse()'><option value='0'>请选择机构</option></select>");
//    $("#orgBox").append(orgSelect);
//    putOptions(orgSelect, "", orgInfo.list);
//    orgSelect.select2();

    function putOptions(orgSelect, supername, list) {
        if (supername != "") supername = supername + "-";
        for (var i = 0; i < list.length; i++) {
            orgSelect.append("<option value=" + list[i].id + ">" + supername + list[i].name + "</option>")
            if (list[i].childrenOrg.length > 0) {
                putOptions(orgSelect, supername + list[i].name, list[i].childrenOrg);
            }
        }
    }
    //申请人
    getStaffList(null,"editForm1 #getStaffId");
    //审核人
    getStaffList(null,"editForm3 #checkStaffId");
    //使用部门
    putOptions($("#editForm1 #useOrgId"), "", orgInfo.list);
    //审核部门
    putOptions($("#editForm3 #checkOrgId"), "", orgInfo.list);
    function useOrgIdChange(initValue) {
//        var orgId=$("#editForm1 #useOrgId").val();
//        //使用人
//        getStaffList(orgId,"editForm1 #useStaffId",initValue);
    }
    function checkOrgIdChange(initValue) {
        var orgId=$("#editForm3 #checkOrgId").val();
        //使用人
        getStaffList(orgId,"editForm3 #checkStaffId",initValue);
    }
    function  useStaffIdChange(){
        var tel=$("#editForm1 #useStaffId option:selected").html().split("[");
        $("#editForm1 #useTel").val(tel[1].replace("]",""));
    }
    function  getStaffIdChange(){
        var tel=$("#editForm1 #getStaffId option:selected").html().split("[");
        $("#editForm1 #getStaffTel").val(tel[1].replace("]",""));
    }
    var mainTable = $('#mainTable').winningTable({
        columns: [
            {'sWidth': '50px', 'title': '状态', 'data': 'stateName'},
            {'sWidth': '50px', 'title': '类型', 'data': 'useTypeName'},
            {'sWidth': '100px', 'title': '使用部门', 'data': 'useOName'},
            {'sWidth': '100px', 'title': '使用人', 'data': 'useSName'},
            {'sWidth': '150px', 'title': '使用电话', 'data': 'useTel'},
            {'sWidth': '150px', 'title': '使用位置', 'data': 'usePosition'},
            {'sWidth': '200px', 'title': '使用原因', 'data': 'reason'},
            {'sWidth': '100px', 'title': '领取人', 'data': 'getSName'},
            {'sWidth': '100px', 'title': '领取人电话', 'data': 'getStaffTel'},
            {'sWidth': '100px', 'title': '审核部门', 'data': 'checkOName'},
            {'sWidth': '100px', 'title': '审核人', 'data': 'checkSName'},
            {'sWidth': '100px', 'title': '审核时间', 'data': 'checkTime'},
            {'sWidth': '100px', 'title': '流水号', 'data': 'flowCode'},
            {'sWidth': '100px', 'title': '创建时间', 'data': 'createTime'},
            {'sWidth': '100px', 'title': '修改时间', 'data': 'updateTime'}
        ],
        usePager: true,
        "order": [[13, "desc"]],
        "paging": true,
        pageLength: 3
    });
    var mainTable2 = $('#mainTable2').winningTable({
        columns: [
            {'sWidth': '100px', 'title': '分类', 'data': 'typeName'},
            {'sWidth': '100px', 'title': '品牌', 'data': 'brandName'},
            {'sWidth': '200px', 'title': '型号', 'data': 'name'},
            {'sWidth': '100px', 'title': '数量', 'data': 'getCount'},
            {'sWidth': '100px', 'title': '已领取', 'data': 'changeCount'},
            {'sWidth': '100px', 'title': '未领取', 'data': 'getCount'},
            {'sWidth': '100px', 'title': '备注', 'data': 'tips'}
        ],
        usePager: false,
        "paging": false,
        pageLength: 10,
        searching: false,
        "rowCallback": function (row, data, index) {
            $('td', row).eq(5).html(data.getCount - data.changeCount);
        }
    });
    var mainTable3 = $('#mainTable3').winningTable({
        columns: [
            {'sWidth': '100px', 'title': '变动类型', 'data': 'typeName'},
            {'sWidth': '100px', 'title': '流水号', 'data': 'flowCode'},
            {'sWidth': '100px', 'title': '资产类型', 'data': 'eqClassName'},
            {'sWidth': '100px', 'title': '品牌', 'data': 'brandName'},
            {'sWidth': '120px', 'title': '型号名称', 'data': 'name'},
            {'sWidth': '100px', 'title': '变动数量', 'data': 'count'},
            {'sWidth': '100px', 'title': '变动状态', 'data': 'stateName'},
            {'sWidth': '100px', 'title': '创建时间', 'data': 'createTime'},
            {'sWidth': '100px', 'title': '修改时间/生效时间', 'data': 'updateTime'}
        ],
        usePager: false,
        "paging": false,
        "order": [[7, "desc"]],
        pageLength: 10,
        searching: false
    });
    refreshWarehouse();
    // 仓库查询
    function refreshWarehouse() {
        $.ajax({
            type: 'POST',
            data: {},
            url: "${request.contextPath}/interface/getMyWarehouse",
            success: function (data) {
                var data = data.datas;
                //成功之后
                var boxs = $("#warehouseBox");
                boxs.html("");
                if (data.length > 0) {
                    //动态加载下拉框
                    var select = $("<select class='form-control' id='warehouseSelect'> <option value=0>全部仓库</option> </select>");
                    boxs.append(select);
                    for (var i = 0; i < data.length; i++) {
                        select.append("<option value='" + data[i].id + "'>" +data[i].name + "</option>");
                    }
                    select.select2();
                }
            },
            error: function (xhr, type) {
                swal("错误", "系统发生内部错误!请稍后再试!", "error");
            }
        });
    }
    // 人员名单查询
    function getStaffList(superOrgId,showSelectId,initValue) {
        $.ajax({
            type: 'POST',
            data: {
                datas: JSON.stringify({
                    organizationId: superOrgId
                })
            },
            url: "${request.contextPath}/cust/staff/queryList",
            success: function (data) {
                var data = data.datas;
                //成功之后
                var select = $("#"+showSelectId);
                select.html("<option value=0>选择人员</option>");
                if (data.length > 0) {
                    for (var i = 0; i < data.length; i++) {
                        select.append("<option value='" + data[i].id + "' "+((initValue!=null&&initValue==data[i].id)?" selected='selected' ":"")+">" + data[i].name + "[" + data[i].telephone + "]</option>");
                    }
                }
            },
            error: function (xhr, type) {
                swal("错误", "系统发生内部错误!请稍后再试!", "error");
            }
        });
    }
    //==================================================end======================================================================================
    function getWarehouseId() {
        if ($("#oneOrganization").val() == 0) {
            return null;
        } else {
            if ($("#warehouseSelect").length == 0) {
                return "0";
            }
            if ($("#warehouseSelect").val() == "0") {
                var value = "";
                for (var i = 0; i < $("#warehouseSelect option").length; i++) {
                    value += ("," + $("#warehouseSelect option").eq(i).attr("value"));
                }
                return value.substr(1);
            } else {
                return $("#warehouseSelect").val();
            }
        }
    }

    // 刷新1
    function refreshData1() {
        if ($("#oneOrganization").val() == 0) {
            swal("错误", "请选择机构", "error");
            return;
        }
        mainTable.queryData("${request.contextPath}/busi/equipmentGet/queryList", {
            datas: JSON.stringify({
                b_createTime: $("#startdate").val(),
                e_createTime: $("#enddate").val(),
                warehouseId: getWarehouseId()
            })
        });
    }

    // 刷新2
    function refreshData2(id) {
        if (id == null) {
            var datas = mainTable.getSelectedDatas();
            if (datas.length == 0) return;
            else id = datas[0].id
        }
        mainTable2.queryData("${request.contextPath}/busi/equipmentGet/queryDetailList", {
            datas: JSON.stringify({
                getId: id
            })
        });
    }
    var selectedState;
    // 刷新3
    function refreshData3(id) {
        if (id == null) {
            var datas = mainTable.getSelectedDatas();
            if (datas.length == 0) return;
            else {
                id = datas[0].id
                selectedState=datas[0].state
            }
        }
        mainTable3.queryData("${request.contextPath}/busi/warehouseChange/queryChangeList", {
            datas: JSON.stringify({
                getId: id
            })
        });
    }
    $('#mainTable').on("click", "tr", function () {
        var index = $(this).index();
        if ($(this).hasClass("selected")){
            refreshData2()
            refreshData3()
        }
    });

    // 新增申请
    function newdetail() {
        if ($("#warehouseSelect").length == 0 || $("#warehouseSelect").val() == 0) {
            swal("错误", "请选择仓库", "error");
            return;
        }
        $('#modal-detail').find(".modal-title").text('为【' + $("#warehouseSelect option:selected").html() + '】新增资产型号');
        clearForm($('#modal-detail'));
        $("#modal-detail #warehouseId").val($("#warehouseSelect").val());
        $("#btndetailSave1").unbind();
        $("#btndetailSave1").click(savedetail);
        $('#modal-detail').modal();
    }

    // 修改申请
    function editdetail() {
        var datas = mainTable.getSelectedDatas();
        if (datas == null || datas.length == 0 || datas.length > 1) {
            swal("错误", "请选择一行申请数据", "error");
            return;
        }
        if(datas[0].state!="109001"){
            swal("错误", "审核通过后禁止此操作", "error");
            return;
        }
        var $modal = $("#modal-detail");
        $modal.find(".modal-title").text("修改信息");
        clearForm($modal);
        var data = datas[0];
        initFormItemData($("#editForm1"), data);
        useOrgIdChange(data.useStaffId);
        $modal.modal();
        $("#btndetailSave1").unbind();
        $("#btndetailSave1").click(savedetail);
    }
    //审核
    function checkdetail() {
        var datas = mainTable.getSelectedDatas();
        if (datas == null || datas.length == 0 || datas.length > 1) {
            swal("错误", "请选择一行申请数据", "error");
            return;
        }
        if(datas[0].state!="109001"){
            swal("错误", "审核通过后禁止此操作", "error");
            return;
        }
        var $modal = $("#modal-detail3");
        $modal.find(".modal-title").text("审核");
        clearForm($modal);
        var data = datas[0];
        if(data.checkTime=="") data.checkTime=nowStr;
        initFormItemData($("#editForm3"), data);
        $("#btndetailSave3").unbind();
        $("#btndetailSave3").click(saveCheck);
        $modal.modal();
    }
    // 新增申请明细
    function newdetailList() {
        var datas = mainTable.getSelectedDatas();
        if (datas == null || datas.length == 0 || datas.length > 1) {
            swal("错误", "请选择一行申请数据", "error");
            return;
        }
        if(datas[0].state!="109001"){
            swal("错误", "审核通过后禁止此操作", "error");
            return;
        }
        $('#modal-detail2').find(".modal-title").text('新增申请明细');
        clearForm($('#modal-detail2'));
        $("#modal-detail2 #getId").val(datas[0].id);
        $("#btndetailSave2").unbind();
        $("#btndetailSave2").click(savedetailList);
        $('#modal-detail2').modal();
    }

    // 修改申请明细
    function editdetailList() {
        var datas = mainTable.getSelectedDatas();
        if (datas == null || datas.length == 0 || datas.length > 1) {
            swal("错误", "请选择一行申请数据", "error");
            return;
        }
        if(datas[0].state!="109001"){
            swal("错误", "审核通过后禁止此操作", "error");
            return;
        }
        var datas = mainTable2.getSelectedDatas();
        if (datas == null || datas.length == 0 || datas.length > 1) {
            swal("错误", "请选择一行明细数据", "error");
            return;
        }
        var $modal = $("#modal-detail2");
        $modal.find(".modal-title").text("修改信息");
        clearForm($modal);
        var data = datas[0];
        initFormItemData($("#editForm2"), data);
        loadModel(data.modelId);
        $modal.modal();
        $("#btndetailSave2").unbind();
        $("#btndetailSave2").click(savedetailList);
    }

    // 生成出库单
    function getChangeInfo() {
        if(selectedState!="109002"){
            swal("错误", "领用申请审核后才可以生成出库单！", "error");
            return;
        }
        var datas = mainTable2.getSelectedDatas();
        if (datas == null || datas.length == 0 || datas.length > 1) {
            swal("错误", "请选择一行明细数据", "error");
            return;
        }
        var $modal = $("#modal-detail4");
        $modal.find(".modal-title").text("生成出库单");
        clearForm($modal);
        var data = datas[0];
        initFormItemData($("#editForm4"), data);
        loadModel(data.modelId);
        $modal.modal();
        $("#btndetailSave4").unbind();
        $("#btndetailSave4").click(saveChangeInfo);
    }

    $("#brandCode").bind("change", loadModel);
    $("#oneCode").bind("change", loadModel);

    function loadModel(initValue) {
        $.ajax({
            type: 'POST',
            data: {
                datas: JSON.stringify({
                    oneCode: $("#oneCode").val(),
                    brandCode: $("#brandCode").val()
                })
            },
            url: '${request.contextPath}/busi/equipment/use/queryModel',
            success: function (data) {
                //成功之后
                $("#modelId").children().remove();
                var optionDefault = $("<option value='0'>请选择</option>");
                $("#modelId").append(optionDefault);
                if (data.success) {
                    $(data.datas).each(function (index, item) {
                        var option = $("<option value='" + item.id + "'>" + item.name + "-" + item.code + "</option>");
                        $("#modelId").append(option);
                    })
                    $("#modelId").val(initValue);
                    $('#select2-modelId-container').html($("#modelId").find("option:selected").text());
                }
                else {
                    swal("错误", data.errorMsg, "error");
                }
            },
            error: function (xhr, type) {
                swal("错误", "系统发生内部错误!请稍后再试!", "error");
            }
        })
    }

    // 保存
    function savedetail() {
        swal({
            title: "确定保存",
            text: "是否确定保存此数据",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "是的,确定",
            cancelButtonText: "取消"
        }).then(function (result) {
            if (!result.value)
                return;
            var requestData = $("#modal-detail form").serializeJSON();
            var params = {datas: JSON.stringify(requestData)};
            $.ajax({
                type: 'POST',
                data: params,
                url: '${request.contextPath}/busi/equipmentGet/save',
                success: function (data) {
                    //成功之后
                    if (data.success) {
                        swal({
                            title: "成功",
                            text: "操作成功!",
                            type: "success",
                            showCancelButton: false,
                            confirmButtonText: "确定"
                        }).then(function (result) {
                            $("#modal-detail").modal('hide');
                            refreshData1();
                        });
                    } else {
                        swal("错误", data.errorMsg, "error");
                    }
                },
                error: function (xhr, type) {
                    swal("错误", "系统发生内部错误!请稍后再试!", "error");
                }
            });
        });
    }
    // 保存审核
    function saveCheck() {
        swal({
            title: "确定保存",
            text: "是否确定保存此数据",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "是的,确定",
            cancelButtonText: "取消"
        }).then(function (result) {
            if (!result.value)
                return;
            var requestData = $("#modal-detail3 form").serializeJSON();
            var params = {datas: JSON.stringify(requestData)};
            $.ajax({
                type: 'POST',
                data: params,
                url: '${request.contextPath}/busi/equipmentGet/saveState',
                success: function (data) {
                    //成功之后
                    if (data.success) {
                        swal({
                            title: "成功",
                            text: "操作成功!",
                            type: "success",
                            showCancelButton: false,
                            confirmButtonText: "确定"
                        }).then(function (result) {
                            $("#modal-detail3").modal('hide');
                            refreshData1();
                        });
                    } else {
                        swal("错误", data.errorMsg, "error");
                    }
                },
                error: function (xhr, type) {
                    swal("错误", "系统发生内部错误!请稍后再试!", "error");
                }
            });
        });
    }
    // 删除
    function deldetail() {
        var datas = mainTable.getSelectedDatas();
        if (datas == null || datas.length == 0 || datas.length > 1) {
            swal("错误", "请选择一行申请数据", "error");
            return;
        }
        var data = datas[0];
        var datas = mainTable.getSelectedDatas();
        if(datas[0].state!="109001"){
            swal("错误", "审核通过后禁止此操作", "error");
            return;
        }
        swal({
            title: "确定删除",
            text: "是否确定删除此数据",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "是的,确定",
            cancelButtonText: "取消"
        }).then(function (result) {
            if (!result.value)
                return;
            var requestData = {
                id: data.id
            };
            var params = {datas: JSON.stringify(requestData)};
            $.ajax({
                type: 'POST',
                data: params,
                url: '${request.contextPath}/busi/equipmentGet/delete',
                success: function (data) {
                    //成功之后
                    if (data.success) {
                        swal({
                            title: "成功",
                            text: "操作成功!",
                            type: "success",
                            showCancelButton: false,
                            confirmButtonText: "确定"
                        }).then(function (result) {
                            refreshData1();
                            mainTable2.setDataSource([]);
                            mainTable3.setDataSource([]);
                        });
                    } else {
                        swal("错误", data.errorMsg, "error");
                    }
                },
                error: function (xhr, type) {
                    swal("错误", "系统发生内部错误!请稍后再试!", "error");
                }
            });
        });
    }

    // 保存明细
    function savedetailList() {
        swal({
            title: "确定保存",
            text: "是否确定保存此数据",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "是的,确定",
            cancelButtonText: "取消"
        }).then(function (result) {
            if (!result.value)
                return;
            var requestData = $("#modal-detail2 form").serializeJSON();
            var params = {datas: JSON.stringify(requestData)};
            $.ajax({
                type: 'POST',
                data: params,
                url: '${request.contextPath}/busi/equipmentGet/saveList',
                success: function (data) {
                    //成功之后
                    if (data.success) {
                        swal({
                            title: "成功",
                            text: "操作成功!",
                            type: "success",
                            showCancelButton: false,
                            confirmButtonText: "确定"
                        }).then(function (result) {
                            $("#modal-detail2").modal('hide');
                            refreshData2();
                        });
                    } else {
                        swal("错误", data.errorMsg, "error");
                    }
                },
                error: function (xhr, type) {
                    swal("错误", "系统发生内部错误!请稍后再试!", "error");
                }
            });
        });
    }
    // 保存出库单
    function saveChangeInfo() {
        var datas = mainTable2.getSelectedDatas();
        if (datas == null || datas.length == 0 || datas.length > 1) {
            swal("错误", "请选择一行明细数据", "error");
            return;
        }
        swal({
            title: "确定保存",
            text: "是否确定保存此数据",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "是的,确定",
            cancelButtonText: "取消"
        }).then(function (result) {
            if (!result.value)
                return;
            var requestData = $("#modal-detail4 form").serializeJSON();
            requestData.count=requestData.count.trim();
            if(requestData.count==null||requestData.count==""||requestData.count=="0" ){
                swal("错误", "出库数量不能为空！", "error");
                return;
            }
            if(requestData.count>(datas[0].getCount - datas[0].changeCount) ){
                swal("错误", "出库数量不能大于可领取数量", "error");
                return;
            }
            var params = {datas: JSON.stringify(requestData)};
            $.ajax({
                type: 'POST',
                data: params,
                url: '${request.contextPath}/busi/equipmentGet/saveChangeInfo',
                success: function (data) {
                    //成功之后
                    if (data.success) {
                        swal({
                            title: "成功",
                            text: "操作成功!",
                            type: "success",
                            showCancelButton: false,
                            confirmButtonText: "确定"
                        }).then(function (result) {
                            $("#modal-detail4").modal('hide');
                            refreshData2();;
                            refreshData3();
                        });
                    } else {
                        swal("错误", data.errorMsg, "error");
                    }
                },
                error: function (xhr, type) {
                    swal("错误", "系统发生内部错误!请稍后再试!", "error");
                }
            });
        });
    }

    // 删除
    function deldetailList() {
        var datas = mainTable.getSelectedDatas();
        if (datas == null || datas.length == 0 || datas.length > 1) {
            swal("错误", "请选择一行申请数据", "error");
            return;
        }
        if(datas[0].state!="109001"){
            swal("错误", "审核通过后禁止此操作", "error");
            return;
        }
        var datas = mainTable2.getSelectedDatas();
        if (datas == null || datas.length == 0 || datas.length > 1) {
            swal("错误", "请选择一行明细数据", "error");
            return;
        }
        var data = datas[0];
        swal({
            title: "确定删除",
            text: "是否确定删除此数据",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "是的,确定",
            cancelButtonText: "取消"
        }).then(function (result) {
            if (!result.value)
                return;
            var requestData = {
                id: data.id
            };
            var params = {datas: JSON.stringify(requestData)};
            $.ajax({
                type: 'POST',
                data: params,
                url: '${request.contextPath}/busi/equipmentGet/deleteList',
                success: function (data) {
                    //成功之后
                    if (data.success) {
                        swal({
                            title: "成功",
                            text: "操作成功!",
                            type: "success",
                            showCancelButton: false,
                            confirmButtonText: "确定"
                        }).then(function (result) {
                            refreshData2();
                        });
                    } else {
                        swal("错误", data.errorMsg, "error");
                    }
                },
                error: function (xhr, type) {
                    swal("错误", "系统发生内部错误!请稍后再试!", "error");
                }
            });
        });
    }
    // 删除
    function delChangeInfo() {
        var datas = mainTable3.getSelectedDatas();
        if (datas == null || datas.length == 0 || datas.length > 1) {
            swal("错误", "请选择一行出库单数据", "error");
            return;
        }
        var data = datas[0];
        if (data.state!="334001") {
            swal("错误", "此变动单已生效禁止此操作！", "error");
            return;
        }
        swal({
            title: "确定删除",
            text: "是否确定删除此数据",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "是的,确定",
            cancelButtonText: "取消"
        }).then(function (result) {
            if (!result.value)
                return;
            var requestData = {
                id: data.id
            };
            var params = {datas: JSON.stringify(requestData)};
            $.ajax({
                type: 'POST',
                data: params,
                url: '${request.contextPath}/busi/warehouseChange/delete',
                success: function (data) {
                    //成功之后
                    if (data.success) {
                        swal({
                            title: "成功",
                            text: "操作成功!",
                            type: "success",
                            showCancelButton: false,
                            confirmButtonText: "确定"
                        }).then(function (result) {
                            refreshData2();
                            refreshData3();
                        });
                    } else {
                        swal("错误", data.errorMsg, "error");
                    }
                },
                error: function (xhr, type) {
                    swal("错误", "系统发生内部错误!请稍后再试!", "error");
                }
            });
        });
    }

    // 打印get
    function printGet() {
        var datas = mainTable.getSelectedDatas();
        if (datas == null || datas.length == 0 || datas.length > 1) {
            swal("错误", "请选择一行申请数据", "error");
            return;
        }
        var data = datas[0];
        window.open('${request.contextPath}/busi/equipmentGet/printGet/'+data.id);
    }
    // 打印change
    function printChange() {
        var datas = mainTable.getSelectedDatas();
        if (datas == null || datas.length == 0 || datas.length > 1) {
            swal("错误", "请选择一行申请数据", "error");
            return;
        }
        var data = datas[0];
        var dataList = mainTable3.getSelectedDatas();
        if (dataList == null || dataList.length == 0 ) {
            swal("错误", "请选择出库单数据", "error");
            return;
        }
        var ids="";
        for(var i=0;i<dataList.length;i++){
            ids+=(","+dataList[i].id)
        }
        window.open('${request.contextPath}/busi/equipmentGet/printChange/'+data.id+"/"+ids.substr(1));
    }
    // 打印二维码
    function printQRCode() {
        var datas = mainTable3.getSelectedDatas();
        if (datas == null || datas.length == 0 || datas.length > 1) {
            swal("错误", "请选择一行出库数据", "error");
            return;
        }
        var data = datas[0];
        if (data.state!="334002") {
            swal("错误", "未生效，无法打印！", "error");
            return;
        }
        window.open('${request.contextPath}/QRcode/information/printByChange/'+data.id);
    }
    // 生效
    function changeUpdate() {
        var datas = mainTable3.getSelectedDatas();
        if (datas == null || datas.length == 0 || datas.length > 1) {
            swal("错误", "请选择一条出库单数据", "error");
            return;
        }
        var data = datas[0];
        if (data.state!="334001") {
            swal("错误", "此变动单已生效请勿重复操作！", "error");
            return;
        }
        swal({
            title: "确定生效",
            text: "是否确定让此变动单生效，生效后库存发生变化，且不能修改或删除变动单。",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "是的,确定",
            cancelButtonText: "取消"
        }).then(function (result) {
            if (!result.value)
                return;
            var requestData = data;
            var params = {datas: JSON.stringify(requestData)};
            $.ajax({
                type: 'POST',
                data: params,
                url: '${request.contextPath}/busi/warehouseChange/update',
                success: function (data) {
                    //成功之后
                    if (data.success) {
                        swal({
                            title: "成功",
                            text: "操作成功!",
                            type: "success",
                            showCancelButton: false,
                            confirmButtonText: "确定"
                        }).then(function (result) {
                            refreshData3();
                        });
                    } else {
                        swal("错误", data.errorMsg, "error");
                    }
                },
                error: function (xhr, type) {
                    swal("错误", "系统发生内部错误!请稍后再试!", "error");
                }
            });
        });
    }
</script>
</@layout.foot>
</body>
</html>